package com.moxi.mogublog.redis.utils;

import com.alibaba.fastjson.JSON;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.Reader;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 数据类型转换
 *
 * @author Lishuai
 * @version 1.0.0
 * @date 2020-04-21
 */
public class DataTypeConvert {

    /**
     * clob转换为string
     *
     * @param clob
     * @return
     */
    public static String clobToString(java.sql.Clob clob) {
        String str = "";
        Reader is = null;
        try {
            if (clob != null) {
                is = clob.getCharacterStream();
                BufferedReader br = new BufferedReader(is);
                String s = br.readLine();
                while (s != null) {
                    str += s;
                    s = br.readLine();
                }
            } else {
                return str;
            }
        } catch (IOException ex) {
            ex.printStackTrace();
            return str;
        } catch (SQLException ex) {
            return "";
        } finally {
            if (is != null) {
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return str;
    }


    /**
     * bean对象换为字符串转
     *
     * @param value
     * @param <T>
     * @return
     */
    public static <T> String beanToString(T value) {
        if (value == null) {
            return null;
        }
        Class<?> clazz = value.getClass();
        if (clazz == int.class || clazz == Integer.class) {
            return "" + value;
        } else if (clazz == String.class) {
            return (String) value;
        } else if (clazz == long.class || clazz == Long.class) {
            return "" + value;
        } else {
            return JSON.toJSONString(value);
        }
    }

    /**
     * 字符串转换为bean对象
     *
     * @param str
     * @param clazz
     * @param <T>
     * @return
     */
    @SuppressWarnings("unchecked")
    public static <T> T stringToBean(String str, Class<T> clazz) {
        if (str == null || str.length() <= 0 || clazz == null) {
            return null;
        }
        if (clazz == int.class || clazz == Integer.class) {
            return (T) Integer.valueOf(str);
        } else if (clazz == String.class) {
            return (T) str;
        } else if (clazz == long.class || clazz == Long.class) {
            return (T) Long.valueOf(str);
        } else if (clazz == List.class) {
            return (T) JSON.parseArray(str);
        } else {
            return JSON.toJavaObject(JSON.parseObject(str), clazz);
        }
    }

    /**
     * Map<String, Object> 转 Map<String, String>
     *
     * @param values
     * @return
     */
    public static Map<String, String> stringObjectMapToString(Map<String, Object> values) {
        Map<String, String> result = new HashMap<String, String>(values.size());
        values.forEach((k, v) -> result.put(k, JSON.toJSONString(v)));
        return result;
    }

}
